package com.example.myproject.seriable;

import com.example.myproject.seriable.qiantao.SerialA;
import com.example.myproject.seriable.qiantao.SerialASub;
import com.example.myproject.seriable.qiantao.SerialB;
import com.example.myproject.util.FileUtil;

/**
 * Created by Administrator on 2018/1/10.
  * jdk自带的序列化默认加入了属性之后会有问题，serailid蛔虫
 * 会报 ava.io.InvalidClassException: com.example.myproject.seriable.SeriableDemo; local class incompatible: stream classdesc serialVersionUID = -7907168010577251448, local class serialVersionUID = 7365906506166784330
 *
 */
public class TestDeSeriableWhenAddNewProperty {

    public static void testA(){
        SeriableDemo test = new SeriableDemo();
        test.setUserName("sds");
        //FileUtil.writeObject("e:/test.data",test);
        SeriableDemo gg = (SeriableDemo) FileUtil.readObject("e:/test.data");
        System.out.println("gg data:"+gg.getUserName());
    }

    /**测试嵌套的子类没有序列化的问题
     * SerialA加入了序列化id
     * SerialB目前没加入，如果B改变了属性了怎么办？
     * SerialB嵌套的情况加入属性，在反序列化会报错吗？
     * 答案：会报错
     * 答案：com.example.myproject.seriable.qiantao.SerialB; local class incompatible: stream classdesc serialVersionUID = -7967651109632198383, local class serialVersionUID = -4277390375448899174
     */
   static  class TestQianTaoSubNoSeriable{
        public static void write(){
            SerialA serialA = new SerialA();
            SerialB serialB = new SerialB();
            serialB.setUserName("sdsd");
            serialA.setSerialB(serialB);
            FileUtil.writeObject("e:/test.data",serialA);
        }
       public static void readWhenChangeSerialAttr(){
           SerialA gg = (SerialA) FileUtil.readObject("e:/test.data");
           System.out.println("gg data:" + gg.getSerialB().getUserName());
       }
    }

    //测试发现不能集成这个serialVersionUID,也是报属性转换错误问题
    //.example.myproject.seriable.qiantao.SerialASub; local class incompatible: stream classdesc serialVersionUID = -9031460467644535529, local class serialVersionUID
    static  class TestExtendDefaultOne{
        public static void write(){
            SerialASub serialA = new SerialASub();
            serialA.setTt("t");
            FileUtil.writeObject("e:/test.data",serialA);
        }
        public static void readWhenChangeSerialAttr(){
            SerialASub gg = (SerialASub) FileUtil.readObject("e:/test.data");
            System.out.println("gg data:" + gg.getTt());
        }
    }

    public static void main(String[] args) {
        //TestQianTaoSubNoSeriable.write();
      TestExtendDefaultOne.readWhenChangeSerialAttr();
    }
}
